package com.alone.embedded.lucene.quickstart.natively;

import com.alone.embedded.lucene.constant.FieldType;
import com.alone.embedded.lucene.core.EmbeddedLucene;
import com.alone.embedded.lucene.core.EmbeddedLuceneConfig;
import com.alone.embedded.lucene.quickstart.Article;
import com.alone.embedded.lucene.repository.ElRepository;
import com.alone.embedded.lucene.repository.handler.AutoFillHandler;
import com.alone.embedded.lucene.repository.handler.DefaultAutoFillValProvider;
import com.alone.embedded.lucene.repository.handler.LogHandler;
import com.alone.embedded.lucene.repository.query.PageQuery;
import com.alone.embedded.lucene.transaction.ElUserTransaction;
import lombok.SneakyThrows;
import org.wltea.analyzer.lucene.IKAnalyzer;

import javax.transaction.UserTransaction;
import java.util.Collections;
import java.util.List;

/**
 * @author Alone
 * @date 2022/8/26 2:53 下午
 */
public class NativeLauncher {

    public static void main(String[] args) {
        EmbeddedLucene lucene = initEmbeddedLucene();
        transaction(lucene);
        crud(lucene);
    }

    private static EmbeddedLucene initEmbeddedLucene() {
        EmbeddedLucene lucene = EmbeddedLucene.create(
                EmbeddedLuceneConfig.builder()
                        // 数据落盘的位置
                        .indexPath("./embedded-lucene-data")
                        // 注册映射文档的JAVA类
                        .registerSource(Article.class)
                        // 为FieldType设置对应解析器
                        .fieldTypeAnalyzer(FieldType.TEXT, new IKAnalyzer(true))
                        // 仓库类扩展插件
                        .repositoryHandler(new LogHandler())
                        .repositoryHandler(new AutoFillHandler(new DefaultAutoFillValProvider()))
                        .build());
        lucene.start();
        return lucene;
    }

    /**
     * 事务功能
     */
    @SneakyThrows
    private static void transaction(EmbeddedLucene lucene) {
        // init data
        ElRepository<Article> repository = ElRepository.get(lucene.getSource(Article.class));
        repository.delete();

        UserTransaction transaction = new ElUserTransaction(lucene.getTxManager());
        transaction.begin();

        repository.insert(Article.DATA);
        System.out.println("未提交读。。。。。。。。");
        System.out.println(repository.selectPage(PageQuery.of(2, 1)));
        System.out.println(repository.select(2L));

        transaction.commit();
        System.out.println("已提交读。。。。。。。。");
        System.out.println(repository.selectPage(PageQuery.of(2, 1)));
        System.out.println(repository.select(2L));

    }

    /**
     * 增删改查
     */
    private static void crud(EmbeddedLucene lucene) {
        // init data
        ElRepository<Article> repository = ElRepository.get(lucene.getSource(Article.class));
        List<Article> data = repository.selectList();
        repository.delete(data);
        repository.insert(Article.DATA);
        repository.delete(1L);

        Article article = repository.select(2L);
        article.setContent("updated content");
        repository.update(Collections.singleton(article));

        repository.select(1L);
        repository.selectList();
        repository.selectPage(PageQuery.of(1, 10));

    }

}
